﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Luna小店.service;
using Seagull.BarTender.Print;
using System.Drawing.Printing;
using Luna小店.report;
using DevExpress.XtraReports.UI;
using DevExpress.Xpf.Printing;
using DevExpress.XtraPrinting;

namespace Luna小店.快递打印
{
    public partial class XtraForm运单打印 : DevExpress.XtraEditors.XtraForm
    {
        private String post_code;   //  邮编
        private String formarea;//目的地
        List<快递打印信息DTO> 快递打印信息DTOs = new List<快递打印信息DTO>();

        public XtraForm运单打印()
        {
            InitializeComponent();
        }

        private void textEdit1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                DataRow dataRow = new 快递Service().get交易单号信息(this.textEdit1.Text);

                this.textBox交易单号.Text = dataRow["QR_NAME"].ToString();
                this.textBox姓名.Text = dataRow["QR_OTHER26"].ToString();
                this.textBox电话.Text = dataRow["QR_OTHER27"].ToString();
                this.textBox收货地址.Text = dataRow["Address"].ToString();
                //formarea = ;
                this.post_code = dataRow["QR_OTHER32"].ToString();

                textBox6.Text = new 快递Service().waybillcode_get();

            }
        }

        /// <summary>
        /// 上传订单信息
        /// </summary>
        private 京配地址DTO uploadInfo()
        {
            new 快递Service().waybill_send(this.textEdit1.Text, this.textBox姓名.Text, this.textBox收货地址.Text, this.textBox电话.Text, this.post_code);
            return new 快递Service().range_check(this.textEdit1.Text, this.textBox收货地址.Text);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //  上传快递信息
            京配地址DTO 京配地址DTO = this.uploadInfo();

            this.devReport打印(京配地址DTO);

            ////  bartender打印
            //this.bartenderPrint();
        }

        private void devReport打印(京配地址DTO 京配地址DTO)
        {
            this.快递打印信息DTOs.Add(
                new 快递打印信息DTO(
                    this.textBox收货地址.Text.Trim(), formarea, this.textBox姓名.Text.Trim(), this.textBox姓名.Text, this.textBox电话.Text, textBox6.Text, textBox6.Text
                    , 京配地址DTO
                    )                
                );
            //Report快递 report = new Report快递();
            Reportxxxx report = new Reportxxxx();
            report.DataSource = this.快递打印信息DTOs;

            //  打印方法
            //report.PrintingSystem.StartPrint += PrintingSystem_StartPrint;
            report.PrintingSystem.PrintProgress += Report_printProgress;
            //report.PrintProgress += Report_printProgress;
            PrintHelper.Print(report);

            // 显示预览
            //ReportPrintTool tool = new ReportPrintTool(report);
            //tool.PrinterSettings.DefaultPageSettings.PaperSize = new PaperSize("8.5x13", 100, 113);
            //tool.ShowPreview();
            //tool.Print();
        }

        private void Report_printProgress(object sender, PrintProgressEventArgs e)
        {
            if (e.PageIndex == 1)
                e.PageSettings.Landscape = true;
        }

        private void PrintingSystem_StartPrint(object sender, PrintDocumentEventArgs e)
        { // Set the page range.
            e.PrintDocument.PrinterSettings.PrintRange = PrintRange.CurrentPage;
            e.PrintDocument.PrinterSettings.FromPage = 1; 
            e.PrintDocument.PrinterSettings.ToPage = 1; 
        } 

            private void 瞎鸡巴打印() {
            var printDocument = new PrintDocument();
            //指定打印机
            printDocument.PrinterSettings.PrinterName = "Microsoft XPS Document Writer";
            //设置页边距
            printDocument.PrinterSettings.DefaultPageSettings.Margins.Left = 0;
            printDocument.PrinterSettings.DefaultPageSettings.Margins.Top = 0;
            printDocument.PrinterSettings.DefaultPageSettings.Margins.Right = 0;
            printDocument.PrinterSettings.DefaultPageSettings.Margins.Bottom = 0;
            //设置尺寸大小，如不设置默认是A4纸
            //A4纸的尺寸是210mm×297mm，
            //当你设定的分辨率是72像素/英寸时，A4纸的尺寸的图像的像素是595×842
            //当你设定的分辨率是150像素/英寸时，A4纸的尺寸的图像的像素是1240×1754
            //当你设定的分辨率是300像素/英寸时，A4纸的尺寸的图像的像素是2479×3508，
            printDocument.DefaultPageSettings.PaperSize = new PaperSize("A4", 595, 842);

            printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
            try
            {
                printDocument.Print();
            }
            catch (InvalidPrinterException)
            {

            }
            finally
            {
                printDocument.Dispose();
            }
        }
        void printDocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            Font titleFont = new Font("黑体", 11, System.Drawing.FontStyle.Bold);//标题字体           
            Font fntTxt = new Font("宋体", 10, System.Drawing.FontStyle.Regular);//正文文字         
            Font fntTxt1 = new Font("宋体", 8, System.Drawing.FontStyle.Regular);//正文文字           
            System.Drawing.Brush brush = new SolidBrush(System.Drawing.Color.Black);//画刷           
            System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black);           //线条颜色         

            try
            {
                //  这部分要改内容和坐标，bartender里面是拖拽的，这他妈对着bartender里面像素对着写吧
                e.Graphics.DrawString("标题name", titleFont, brush, new System.Drawing.Point(20, 10));
                e.Graphics.DrawString("资产编号：", fntTxt, brush, new System.Drawing.Point(20, 31));
                e.Graphics.DrawString("123456789123465", fntTxt, brush, new System.Drawing.Point(80, 31));
                e.Graphics.DrawString("资产序号：", fntTxt, brush, new System.Drawing.Point(20, 46));
                e.Graphics.DrawString("123456789131321", fntTxt, brush, new System.Drawing.Point(80, 46));
                e.Graphics.DrawString("底部name", fntTxt1, brush, new System.Drawing.Point(100, 62));

            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }

            //如果打印还有下一页，将HasMorePages值置为true;
            e.HasMorePages = false;
        }

        private void bartenderPrint() {
            Engine btEngine = new Engine();
            btEngine.Start();
            //string lj = AppDomain.CurrentDomain.BaseDirectory + "顺丰订单模板.btw";  //test.btw是BT的模板
            //string lj = AppDomain.CurrentDomain.BaseDirectory + "001.btw";  //test.btw是BT的模板

            LabelFormatDocument btFormat = btEngine.Documents.Open("C:\\发货模板.btw");
            //指定打印机名 
            //btFormat.PrintSetup.PrinterName = "HPRT HLP106S-UE";
            //btFormat.PrintSetup.PrinterName = "TEC";
            btFormat.PrintSetup.PrinterName = "Deli DL-888B(NEW)";
            //打印份数                   
            btFormat.PrintSetup.IdenticalCopiesOfLabel = 1;
            //改变标签打印数份连载 
            btFormat.PrintSetup.NumberOfSerializedLabels = 1;
            //对BTW模版相应字段进行赋值 
            btFormat.SubStrings["customerInformation"].Value = this.textBox收货地址.Text.Trim();
            btFormat.SubStrings["area"].Value = formarea;
            btFormat.SubStrings["customer"].Value = this.textBox姓名.Text.Trim();

            btFormat.SubStrings["kehu"].Value = this.textBox姓名.Text;

            btFormat.SubStrings["phone"].Value = this.textBox电话.Text;
            btFormat.SubStrings["yun_dan_hao"].Value = textBox6.Text;
            btFormat.SubStrings["yundanhao1"].Value = textBox6.Text;

            Messages messages;
            int waitout = 10000; // 10秒 超时 
            Result nResult1 = btFormat.Print("运单" + this.textBox交易单号, waitout, out messages);
            btFormat.PrintSetup.Cache.FlushInterval = CacheFlushInterval.PerSession;
            //不保存对打开模板的修改 
            btFormat.Close(SaveOptions.DoNotSaveChanges);
            //结束打印引擎                  
            btEngine.Stop();
        }
    }

    class 快递打印信息DTO {
        public String customerInformation { get; set; }
        public String area { get; set; }
        public String customer { get; set; }
        public String kehu { get; set; }
        public String phone { get; set; }
        public String yun_dan_hao { get; set; }
        public String yundanhao { get; set; }

        #region 京配地址DTO
        public String sourcetSortCenterId { get; set; } //	820864	始发分拣中心编码
        public String sourcetSortCenterName { get; set; } // 东莞亚一分拣中心    始发分拣中心名称
        public String targetSortCenterName { get; set; } //  兰州分拨中心 目的分拣中心名称
        public String road { get; set; } //	99E	始发道口号
        public String siteName { get; set; } //	◆  兰州兰工坪营业部 目的站点名称
        public String destinationTabletrolleyCode { get; set; } //	58176	目的分拣中心编码
        #endregion


        public 快递打印信息DTO(String customerInformation, String area, String customer, String kehu, String phone, String yun_dan_hao, String yundanhao, 京配地址DTO 京配地址DTO) {
            this.customerInformation = customerInformation;
            this.area = area;
            this.customer = customer;
            this.kehu = kehu;
            this.phone = phone;
            this.yun_dan_hao = yun_dan_hao;
            this.yundanhao = yundanhao+ "-1-1-";


            this.sourcetSortCenterId = 京配地址DTO.sourcetSortCenterId;
            this.sourcetSortCenterName = 京配地址DTO.sourcetSortCenterName;
            this.targetSortCenterName = 京配地址DTO.targetSortCenterName;
            this.road = 京配地址DTO.road;
            this.siteName = 京配地址DTO.siteName;
            this.destinationTabletrolleyCode = 京配地址DTO.destinationTabletrolleyCode;
        }
    }
}